/*
 * @Author: shawnXiao
 * @Date: 2021-12-18 21:16:31
 * @LastEditTime: 2021-12-19 22:26:48
 * @FilePath: /vue3-vite2-ts-antdv/src/router/index.ts
 */
// @/router/index.ts
import { createRouter,createWebHashHistory,RouteRecordRaw} from "vue-router";

const asyncImport = (file: string) => () => import('../views/' + file + '.vue')
const asyncImportTsx = (file: string) => () => import('../views/' + file + '.tsx')

//  静态无需权限路由
export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    component: asyncImport('Home/index'),
    name: 'Home',
    meta: {
      title: '疫情面板',
      icon: '',
      noCache: true,
      hidden: false,
      permissions: []
    }
  },
  {
    path: '/jsx',
    component: asyncImport('Jsx/index'),
    name: 'Jsx',
    meta: {
      title: 'Jsx',
      icon: '',
      noCache: true,
      hidden: false,
      permissions: []
    }
  },
  {
    path: '/tsx',
    component: asyncImportTsx('Tsx/index'),
    name: 'Tsx',
    meta: {
      title: 'Tsx',
      icon: '',
      noCache: true,
      hidden: false,
      permissions: []
    }
  },
    {
    path: '/login',
    component: asyncImport('Login/index'),
    name: 'Login',
    meta: {
      title: 'Login',
      icon: '',
      noCache: true,
      hidden: true,
      permissions: []
    }
  },
  {
    path: '/403',
    name: '403',
    meta: {
      title: '403',
      icon: '',
      noCache: true,
      hidden: true,
      permissions: []
    },
    component: asyncImport('Error/403')
  },
  {
    path: '/404',
    name: '404',
    meta: {
      title: '404',
      icon: '',
      noCache: true,
      hidden: true,
      permissions: []
    },
    component: asyncImport('Error/404')
  },
  // vue3对404配置进行了修改,必须要使用正则匹配
  { path: '/:pathMatch(.*)*', redirect: '/404' },
  { path: '', redirect: '/home' }
]

// 异步权限路由
export const asyncRoutes = []

const scrollBehavior: any = (to: any, from: any, savedPosition: number) => {
  if (savedPosition) {
    return savedPosition
  }
  if (!from.meta.noCache) {
    from.meta.savedPosition = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset
  }
  return { left: 0, top: to.meta.savedPosition || 0 }
}

const router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior,
  routes: constantRoutes
})

router.beforeEach((to, from) => {
    // to.path = '/login'
    return true
})

export default router;
